package com.dlc.commonlibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlc.commonlibrary.R;
import com.dlc.commonlibrary.utils.DialogUtil;

/**
 * 显示正在操作以及操作结果的dialog
 */
public class ReadNumsDialog extends Dialog {

    private final Context mContext;
    private final ImageView mIvMark;
    private final TextView mTvMessage;
    private final Handler mHandler;
    private final TextView mTvDots;
    private final Button mBtnOk;
    private int mSuccessImageResId;
    private int mFailedImageResId;
    private CharSequence mWorkingText;
    private CharSequence mSuccessText;
    private CharSequence mFailedText;
    private int mDotCount;

    private int rfidnums = 0;
    private OnClickButtonListener mOnClickButtonListener;

    private final Runnable mDismissRunnable = new Runnable() {
        @Override
        public void run() {
            dismiss();
        }
    };

    private final Runnable mDotRunnable = new Runnable() {
        @Override
        public void run() {

            switch (mDotCount) {
                case 0:
                    mTvDots.setText(rfidnums+"");
                    break;
            }
            mDotCount = (mDotCount + 1) % 4;
            mHandler.postDelayed(mDotRunnable, 500);
        }
    };

    public ReadNumsDialog(@NonNull Context context) {
        super(context, R.style.CommonDialogStyle);
        mHandler = new Handler();
        mContext = context;
        setContentView(R.layout.dialog_read_nums);
        DialogUtil.adjustDialogLayout(this, false, false);
        DialogUtil.setGravity(this, Gravity.CENTER_HORIZONTAL);

        mIvMark = (ImageView) findViewById(R.id.iv_mark);
        mTvMessage = (TextView) findViewById(R.id.tv_message);
        mTvDots = (TextView) findViewById(R.id.tv_dots);
        mBtnOk = (Button) findViewById(R.id.btn_ok);
        mBtnOk.setVisibility(View.GONE);
        mBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickButtonListener != null) {
                    mOnClickButtonListener.onClickButton(ReadNumsDialog.this);
                }
            }
        });
    }

    public static ReadNumsDialog newDialog(Context context) {
        ReadNumsDialog doResultDialog = new ReadNumsDialog(context);
        return doResultDialog;
    }

    public ReadNumsDialog setWorkingText(CharSequence text) {
        mWorkingText = text;
        return this;
    }

    public ReadNumsDialog setSuccessText(CharSequence text) {
        mSuccessText = text;
        return this;
    }

    public ReadNumsDialog setFailedText(CharSequence text) {
        mFailedText = text;
        return this;
    }

    /**
     * 设置按钮
     *
     * @param buttonText 按钮文本
     * @param listener 监听器
     * @return
     */
    public ReadNumsDialog setButton(CharSequence buttonText,
                                    @Nullable OnClickButtonListener listener) {
        mBtnOk.setText(buttonText);
        mOnClickButtonListener = listener;
        return this;
    }

    public ReadNumsDialog setSuccessImage(int resId) {
        mSuccessImageResId = resId;
        return this;
    }

    public ReadNumsDialog setFailedImage(int resId) {
        mFailedImageResId = resId;
        return this;
    }

    public void showWorking() {
        mTvMessage.setText(mWorkingText);
        mIvMark.setVisibility(View.GONE);
        mDotCount = 0;
        mHandler.post(mDotRunnable);
        show();
    }
    public void showWorking(int nums) {
        mTvMessage.setText(mWorkingText);
        mIvMark.setVisibility(View.GONE);
        mDotCount = 0;
        rfidnums = nums;
        mHandler.post(mDotRunnable);
        show();
    }

    /**
     * 显示正在运行
     *
     * @param withButton 是否显示按钮
     */
    public void showWorking(boolean withButton) {
        mBtnOk.setVisibility(withButton ? View.VISIBLE : View.GONE);
        showWorking();
    }

    public void showResultAndDismiss(boolean success, int delay) {
        showResult(success);
        mHandler.postDelayed(mDismissRunnable, delay);
    }

    public void showResultAndRun(boolean success, Runnable runnable, int delay) {
        showResult(success);
        mHandler.postDelayed(runnable, delay);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mHandler.removeCallbacks(mDotRunnable);
        mHandler.removeCallbacks(mDismissRunnable);
    }

    public void showResult(boolean success) {

        mHandler.removeCallbacks(mDotRunnable);

        mTvDots.setText("");

        if (success) {
            mIvMark.setImageResource(mSuccessImageResId);
            mTvMessage.setText(mSuccessText);
        } else {
            mIvMark.setImageResource(mFailedImageResId);
            mTvMessage.setText(mFailedText);
        }
        mIvMark.setVisibility(View.VISIBLE);
        if (!isShowing()) {
            show();
        }
    }

    public interface OnClickButtonListener {
        void onClickButton(ReadNumsDialog dialog);
    }
}
